package com.zhouyibike.uu.ui.activity.share;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetInformation6Result;
import com.zhouyibike.uu.entity.ShareCodeResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.base.MyApplication;
import com.zhouyibike.uu.utils.Util;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.OnekeyShareTheme;

public class MyShareMsgActivity3 extends BaseActivity implements View.OnClickListener {
    private ImageButton mBtnBack;
    private ImageView barshare;
    private TextView mBtnTitle, share1, share2, share3;
    private TextView t1, t2, t3, t4, t5, t7, t8, t9, t10, t6;
    private LinearLayout l2, l3, l4, l5, l7, l8, l9, l10, l6;
    private String shareurl;
    private TextView tv_des, tv_t3;
    private TextView tv_mynumber;
    private String s1 = "点击下方<font color='#FF8624'>立即购买</font>按钮";
    private String s2 = "好友验证手机号，得免费<font color='#FF8624'>骑行券</font>一张";
    private String s3 = "被邀请人认购单车后，邀请人获得认购金额<font color='#FF8624'>20%</font>的奖励";
    private int QR_WIDTH;
    private int QR_HEIGHT;
    private ImageView erweima;
    private Button btoneshot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_share_msg3);
        initView();
        QR_WIDTH = Util.dp2px(this, 200);
        QR_HEIGHT = Util.dp2px(this, 200);
        getMsg();
    }

    private void initView() {
        share1 = (TextView) findViewById(R.id.fenxiangtv1);
        share2 = (TextView) findViewById(R.id.fenxiangtv2);
        share3 = (TextView) findViewById(R.id.fenxiangtv3);
        btoneshot = (Button) findViewById(R.id.oneshotshare);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("邀请分享");
        mBtnBack.setOnClickListener(this);
        btoneshot.setOnClickListener(this);
        erweima = (ImageView) findViewById(R.id.erweima);
    }

    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        // showProgressDialog();
        addSubscription(apiStores.MyShared(params),
                new ApiCallback<ShareCodeResult>() {

                    @Override
                    public void onSuccess(ShareCodeResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            shareurl = model.getData().getLink();
                            createQRImage(shareurl);
                        } else if (model.getStatus() == 508 || model.getStatus() == 506 || model.getStatus() == 307) {
                            setResult(2501);
                            finish();
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                        getMsg2();
                    }
                });
    }

    private void getMsg2() {
        Map<String, String> params = new HashMap<>();
        params.put("type", "6");
        // showProgressDialog();
        addSubscription(apiStores.GetInformation6(params),
                new ApiCallback<GetInformation6Result>() {
                    @Override
                    public void onSuccess(GetInformation6Result model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            String string = model.getData().getDescription();
                            String[] split = string.split("##");
                            s1 = split[0];
                            s2 = split[1];
                            s3 = split[2];
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                        share1.setText(Html.fromHtml(s1));
                        share2.setText(Html.fromHtml(s2));
                        share3.setText(Html.fromHtml(s3));
                        if (share1.getText().toString().length() <= 10) {
                            share1.setTextSize(15);
                        } else if (share1.getText().toString().length() <= 20) {
                            share1.setTextSize(14);
                        } else if (share1.getText().toString().length() <= 30) {
                            share1.setTextSize(12);
                        } else if (share1.getText().toString().length() <= 40) {
                            share1.setTextSize(10);
                        }
                        if (share2.getText().toString().length() <= 10) {
                            share2.setTextSize(15);
                        } else if (share2.getText().toString().length() <= 20) {
                            share2.setTextSize(14);
                        } else if (share2.getText().toString().length() <= 30) {
                            share2.setTextSize(12);
                        } else if (share2.getText().toString().length() <= 40) {
                            share2.setTextSize(10);
                        }
                        if (share3.getText().toString().length() <= 10) {
                            share3.setTextSize(15);
                        } else if (share3.getText().toString().length() <= 20) {
                            share3.setTextSize(14);
                        } else if (share3.getText().toString().length() <= 30) {
                            share3.setTextSize(12);
                        } else if (share3.getText().toString().length() <= 40) {
                            share3.setTextSize(10);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
            case R.id.oneshotshare:
                showShare(MyShareMsgActivity3.this, preferences.getString("logo", ""), shareurl);
                break;
        }
    }

    /**
     * 演示调用ShareSDK执行分享
     */
    public static void showShare(Context context, String imgurl, String shareurl) {
        OnekeyShare oks = new OnekeyShare();
        oks.setSilent(!true);
//        //ShareSDK快捷分享提供两个界面第一个是九宫格 CLASSIC  第二个是SKYBLUE
        oks.setTheme(OnekeyShareTheme.CLASSIC);
        // 令编辑页面显示为Dialog模式
        oks.setDialogMode();
        // 在自动授权时可以禁用SSO方式
        oks.disableSSOWhenAuthorize();
        oks.setTitle("加入周翼单车");
        oks.setTitleUrl(shareurl);
        oks.setText("朋友们，你我一起享受骑行，赶快下载周翼单车APP");
        oks.setImageUrl(imgurl);
        oks.setUrl(shareurl); //微信不绕过审核分享链接
        oks.setComment("朋友们，你我一起享受骑行，赶快下载周翼单车APP"); //我对这条分享的评论，仅在人人网和QQ空间使用，否则可以不提供
        oks.setSite("加入周翼单车");  //QZone分享完之后返回应用时提示框上显示的名称
        oks.setSiteUrl(shareurl);//QZone分享参数
        oks.setVenueName("加入周翼单车");
        oks.setVenueDescription("朋友们，你我一起享受骑行，赶快下载周翼单车APP");
        oks.setLatitude((float) MyApplication.getInstances().getLat());
        oks.setLongitude((float) MyApplication.getInstances().getLng());
        oks.show(context);
    }

    //要转换的地址或字符串,可以是中文
    public void createQRImage(String url) {
        try {
            //判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1) {
                return;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            //下面这里按照二维码的算法，逐个生成二维码的图片，
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }
            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            //显示到一个ImageView上面
            erweima.setImageBitmap(bitmap);
            erweima.setVisibility(View.VISIBLE);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
}
